﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Dao;
using log4net;
using Common.Model;

namespace Analyze.Script
{
    public class BlockFilterScript
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(BlockFilterScript));
        
        public static void Run()
        {
            Log.Info("BlockFilterScript start");
            List<NewBlock> newBlocks = new List<NewBlock>();
            List<string> eventBlock = DxwEventDao.GetEventBlockDistinct();
            List<string> dxwEvents = DxwEventDao.GetEventBlock();
            
            foreach (string block in eventBlock)
            {
                var count = dxwEvents.Count(m => m.Equals(block));
                if (count < 10)
                {
                    newBlocks.Add(new NewBlock()
                    {
                        BlockName = block,
                        CreateTime = DateTime.Now,
                        From = "dxw",
                        ManualHandleStatus = 0,
                        EventCount = count
                    });
                }
            }

            Log.Info("BlockFilterScript block = " + newBlocks.Count);
            
            if (newBlocks.Count > 0)
            {
                NewBlockDao.BatchSaveNewBlocks(newBlocks);
            }
            Log.Info("BlockFilterScript end");
        }
    }
}